import {createSlice} from '@reduxjs/toolkit'

const tableSlice=createSlice({
    name:'table',
    initialState:{
        // 表格数据
        tableData:[], //所有数据
        searchTable:[] //筛选之后的数据
    },
    reducers:{
        // 修改表格数据
        setTable(state,{payload,type}){
           state.tableData=payload;
           state.searchTable=payload;

        },
        //查询
        sTable(state,{payload,type}){
        state.searchTable=   state.tableData.filter((item) =>item.name.includes(payload.val) &&  item.flag.includes(payload.flag) )
        },
        //删除
        delTable(state,{payload,type}){
            state.searchTable.forEach((item,index)=>{
                if(item.key==payload){
                    state.searchTable.splice(index,1)
                }
            })
        },
        //新曾
      addTable(state,{payload,type}){
            state.searchTable.unshift(payload)
      },
        //修改
        editTable(state,{payload,type}){
            state.searchTable.forEach((item)=>{
                if(item.key==payload.key){
                    item.name=payload.name
                    item.flag=payload.flag
                    item.address=payload.address;
                }
            })
        }
    }
})
export const {setTable,sTable,delTable,addTable,editTable}=tableSlice.actions
export default tableSlice.reducer